package cn.kgc.crowdfundingadmin02.controller;

import cn.kgc.crowdfundingadmin02.common.CrowdFundingConstant;
import cn.kgc.crowdfundingadmin02.common.ResultEntity;
import cn.kgc.crowdfundingadmin02.pojo.Admin;
import cn.kgc.crowdfundingadmin02.service.AdminService;
import com.github.pagehelper.PageInfo;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author shkstart
 * @create 2020-02-20 8:58
 */
@Controller
public class AdminController {

    @Resource
    AdminService adminService;


    /*测试搭好的ssm框架是否好用*/
    @RequestMapping("/admin/get_by_id")
    @ResponseBody
    public Admin adminget_by_id(Integer id) {
        Admin admin = adminService.selectByPrimaryKey(id);
        return admin;
    }

    /*管理员登陆 */
 /*   @RequestMapping("/admin/do/login")
    public String doLogin(@RequestParam("loginAcct") String loginAcct,
                          @RequestParam("userPswd") String userPswd,
                          Model model,
                          HttpSession session) {

        Admin admin = adminService.login(loginAcct, userPswd);
        if (admin != null) {//登陆成功
            session.setAttribute("LOGIN-ADMIN", admin);
            *//*  return "admin-main";*//*
            return "redirect:/admin/to/main/page.html";
        } else {//登陆失败
            model.addAttribute(CrowdFundingConstant.ATTR_NAME_MESSAGE, CrowdFundingConstant.MESSAGE_LOGIN_FAILED);
            return "admin-login";
        }
    }*/

    /*异常处理功能*/
    @ExceptionHandler(value = Exception.class)
    public String catchException(Exception exception, Model model) {
        model.addAttribute(CrowdFundingConstant.ATTR_NAME_ERROR_MESSAGE, exception);
        return "system-error";
    }

    /*退出功能*/
    @RequestMapping("/admin/logout")
    public String logout(HttpSession session) {
        session.invalidate();
        return "redirect:/index";
    }


    /*分页查询 */
    @RequestMapping("/admin/query/for/search")
    @PreAuthorize(value = "hasAnyRole('超级管理员','经理')")
    public String queryForSearch(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                 @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                                 @RequestParam(value = "keyword", defaultValue = "") String keyword,
                                 Model model) {
        PageInfo<Admin> pageInfo = adminService.queryForKeywordSearch(pageNum, pageSize, keyword);
        /*     model.addAttribute(CrowdFundingConstant.ATTR_NAME_PAGE_INFO, pageInfo);*/
        model.addAttribute(CrowdFundingConstant.ATTR_NAME_PAGE_INFO, pageInfo);
        return "admin-page";
    }

    //根据id删除
    @RequestMapping("/admin/batch/remove")
    @ResponseBody
    public ResultEntity<String> batchRemove(@RequestBody List<Integer> adminIdList) {
        try {
            adminService.batchRemove(adminIdList);
            return ResultEntity.successWithoutData();
        } catch (Exception e) {
            return ResultEntity.failed(null, e.getMessage());
        }
    }

    //添加
    @RequestMapping("/admin/save")
    public String saveAdmin(Admin admin) {
        adminService.saveAdmin(admin);
        //增加成功跳转到 分页查询
        return "redirect:/admin/query/for/search";
    }

    //修改前的差询
    @RequestMapping("/admin/to/edit/page")
    public String toEditPage(@RequestParam("adminId") Integer adminId,
                             @RequestParam("pageNum") Integer pageNum,Model model) {
        Admin adminById = adminService.getAdminById(adminId);
        model.addAttribute(CrowdFundingConstant.ATTR_Admin,adminById);
        return  "admin-edit";
    }
    //修改
    @RequestMapping("/admin/update")
    public String updateAdmin(Admin admin,Integer pageNum) {
        adminService.updateAdmin(admin);
        return "redirect:/admin/query/for/search?pageNum="+pageNum;
    }
}
